package com.ahstu.learning.phoneContacts;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ahstu.learning.R;
import com.ahstu.learning.base.BaseActivity;
import com.ahstu.learning.phoneContacts.LetterBar.OnLetterSelectedListener;

import java.util.ArrayList;
import java.util.List;

public class tongxunlu extends BaseActivity {

    private LetterBar lb;
    private TextView tv_overlay;
    ImageView phone_title_back;
    private Button btn_add;
    private PinnedSectionListView lv_contacts;

    private ContactAdapter adapter;
    private List<ContactBean> contacts = new ArrayList<ContactBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frendlianxiren);

        lb = (com.ahstu.learning.phoneContacts.LetterBar) findViewById(R.id.lb);
        tv_overlay = (TextView) findViewById(R.id.tv_overlay);
        phone_title_back = (ImageView) findViewById(R.id.phone_title_back);
        lv_contacts = (PinnedSectionListView) findViewById(R.id.lv_contacts);
        lv_contacts.setShadowVisible(false);

        adapter = new com.ahstu.learning.phoneContacts.ContactAdapter(this, contacts);
        lv_contacts.setAdapter(adapter);


        phone_title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lv_contacts.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Object item = adapter.getItem(position);
                if (item instanceof com.ahstu.learning.phoneContacts.ContactBean) {
                    ContactBean contact = (ContactBean) item;
                    showLongClickDialog(contact);
                }

            }
        });

        lv_contacts.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                                           int position, long id) {
                Object item = adapter.getItem(position);
                if (item instanceof com.ahstu.learning.phoneContacts.ContactBean) {
                    ContactBean contact = (ContactBean) item;
                    ContactManager.deleteContact(tongxunlu.this, contact);
                    setContactsData();
                }
                return true;
            }
        });

        lb.setOnLetterSelectedListener(new OnLetterSelectedListener() {

            @Override
            public void onLetterSelected(String letter) {
                if (TextUtils.isEmpty(letter)) {
                    tv_overlay.setVisibility(View.GONE);
                } else {
                    tv_overlay.setVisibility(View.VISIBLE);
                    tv_overlay.setText(letter);

                    int position = adapter.getLetterPosition(letter);
                    if (position != -1) {
                        lv_contacts.setSelection(position);
                    }
                }
            }
        });

        setContactsData();
    }

    private void showLongClickDialog(final com.ahstu.learning.phoneContacts.ContactBean contact) {
        new AlertDialog.Builder(this)
                .setItems(new String[]{"拨打电话", "发送短信"},
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {
                                    case 0:
                                        Intent intentCall = new Intent();
                                        intentCall.setAction(Intent.ACTION_CALL);
                                        intentCall.setData(Uri.parse("tel:" + contact.getPhone()));
                                        startActivity(intentCall);
                                        break;
                                    case 1:
                                        Intent intentSend = new Intent();
                                        intentSend.setAction(Intent.ACTION_SENDTO);
                                        intentSend.setData(Uri.parse("smsto://" + contact.getPhone()));
                                        startActivity(intentSend);
                                        break;

                                }
                            }
                        })
                .show();
    }

    private void setContactsData() {
        List<ContactBean> contactData = ContactManager.getContacts(this);
        if (contactData.isEmpty()) {
            Toast.makeText(tongxunlu.this, "手机不存在联系人", Toast.LENGTH_LONG).show();
        }
        contacts.clear();
        contacts.addAll(contactData);
        adapter.updateList();
    }

}
